#This Makefile originates from the Makefile of the demo program of this project
#And is modified in need of creating a user level thread library.
##################################################################################
#				IMPORTANT NOTICE
#By default, the kernel-level threads' extension is enabled, which provides
#real parallelism by using kernel-level threads.
#To disable the use of this extension, just comment the following line.
#
#CXXFLAGS += -DKERNEL_LEVEL_THREAD_EXTENSION
#
##################################################################################

# specify our compiler and build/link flags...  We use -MMD for dependencies
CXX       = g++
AR	  = ar
LDFLAGS   = -lrt -m32
CXXFLAGS  = -fno-strict-aliasing -Wall -D_REENTRANT -MMD -msse2 -mfpmath=sse \
            -m32 -ggdb -O0 -g -fpermissive 

#comment the line below to disable kernel-level threads extension
CXXFLAGS += -DKERNEL_LEVEL_THREAD_EXTENSION

ARFLAGS   = rcs
MAKE	  = gmake

# specify paths and filenames...  everything goes into OBJDIR
SRCDIR	  = .
OBJDIR    = $(SRCDIR)/obj
BASENAMES = uthread mutex
SOURCES   = $(patsubst %, $(SRCDIR)/%.c, $(BASENAMES))
OFILES    = $(patsubst %, $(OBJDIR)/%.o, $(BASENAMES) )
DEPS      = $(patsubst %, $(OBJDIR)/%.d, $(BASENAMES) )
GOAL      = $(OBJDIR)/uthread.a

# build rules
.PHONY: all clean realclean info

all: info $(OBJDIR) $(GOAL)
	@echo $(GOAL) complete

# It is useful to print the FLAGS, since we hide the actual CC invocations
info:
	@echo "Building with CXXFLAGS=${CXXFLAGS}"
	@echo "and LDFLAGS=${LDFLAGS}"
	@echo "and ARFLAGS=$(ARFLAGS)"

# /clean/ deletes everything from the obj directory
clean:
	@echo "rm -f $(OFILES) $(GOAL)"
	@rm -f $(OFILES) $(GOAL)

# /realclean/ also kills the directory and the dependencies
realclean:
	@rm -rf $(OBJDIR)

# build the directory in which the .o files will go
$(OBJDIR):
	mkdir -p $@

# compilation rule for making a .o file from a cpp
$(OBJDIR)/%.o : $(SRCDIR)/%.c
	@echo [${CXX}] $< "-->" $@
	@$(CXX) $(CXXFLAGS) -c $< -o $@

# build the library archive
$(GOAL) : $(OFILES)
	@echo [${AR}] "${OBJDIR}/*.o -->" $@
	@$(AR) $(ARFLAGS) $(GOAL) $(OFILES)

# convince /make/ not to worry if a .d is missing
$(DEPS):

-include $(DEPS)

